package org.study.shardingsphere.ds.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.study.shardingsphere.ds.mapper.TestModelMapper;
import org.study.shardingsphere.ds.model.TestModel;

import java.util.List;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-05-24
 */
@Service
public class TestModelServiceImpl extends ServiceImpl<TestModelMapper, TestModel> implements TestModelService {
    @Resource
    private TestModelMapper testModelMapper;

    @DS("sharding_ds")
    @Override
    public int insert2Sharding(TestModel testModel) {
        return testModelMapper.insert(testModel);
    }

    @DS("demo_ds_0")
    @Override
    public int insert2Master(TestModel testModel) {
        return testModelMapper.insert(testModel);
    }

    @DS("demo_ds_1")
    @Override
    public int insert2Slave(TestModel testModel) {
        return testModelMapper.insert(testModel);
    }

    @DS("sharding_ds")
    @Override
    public List<TestModel> selectAll() {
        return testModelMapper.selectList(null);
    }

    @DS("demo_ds_0")
    @Override
    public PageInfo<TestModel> startPage1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TestModel> testModelList = testModelMapper.selectList(null);
        PageInfo<TestModel> pageInfo = new PageInfo<>();
        if (testModelList != null) {
            pageInfo = new PageInfo<>(testModelList);
        }
        return pageInfo;
    }

    @DS("demo_ds_1")
    @Override
    public PageInfo<TestModel> startPage2(int pageNum, int pageSize) {
        Page<TestModel> startPage = PageHelper.startPage(pageNum, pageSize);
        List<TestModel> testModelList = testModelMapper.selectList(null);
        PageInfo<TestModel> pageInfo = new PageInfo<>();
        if (testModelList != null) {
            pageInfo = new PageInfo<>(startPage);
        }
        return pageInfo;
    }

    @DS("demo_ds_0")
    @Override
    public PageInfo<TestModel> offsetPage1(int pageNum, int pageSize) {
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<TestModel> testModelList = testModelMapper.selectList(null);
        PageInfo<TestModel> pageInfo = new PageInfo<>();
        if (testModelList != null) {
            pageInfo = new PageInfo<>(testModelList);
        }
        return pageInfo;
    }

    @DS("demo_ds_1")
    @Override
    public PageInfo<TestModel> offsetPage2(int pageNum, int pageSize) {
        Page<TestModel> testModelPage = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<TestModel> testModelList = testModelMapper.selectList(null);
        PageInfo<TestModel> pageInfo = new PageInfo<>();
        if (testModelList != null) {
            pageInfo = new PageInfo<>(testModelPage);
        }
        return pageInfo;
    }

    @DS("demo_ds_0")
    @Override
    public PageInfo<TestModel> doSelectPageJdk7(int pageNum, int pageSize) {
        Page<TestModel> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(new ISelect() {
                    @Override
                    public void doSelect() {
                        testModelMapper.selectList(null);
                    }
                });
        PageInfo<TestModel> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @DS("demo_ds_1")
    @Override
    public PageInfo<TestModel> doSelectPageJdk8(int pageNum, int pageSize) {
        Page<TestModel> page = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(() -> testModelMapper.selectList(null));
        PageInfo<TestModel> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @DS("sharding_ds")
    @Override
    public PageInfo<TestModel> doSelectPageInfo(int pageNum, int pageSize) {
        PageInfo<TestModel> pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> testModelMapper.selectList(null));
        return pageInfo;
    }

    @DS("demo_ds_0")
    @Override
    public long countJdk7() {
        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                testModelMapper.selectList(null);
            }
        });
        return count;
    }

    @DS("demo_ds_1")
    @Override
    public long countJdk8() {
        long count = PageHelper.count(() -> testModelMapper.selectList(null));
        return count;
    }
}
